package com.dyz.leetcode;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WordBreak139 {
    //动态规划
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length()+1];
        dp[0] = true;

        //dp[i]表示前i个字符能否被拼接成功
        //status function： dp[i] = (dp[j]==true && s.substring(j,i))
        Set<String> set = new HashSet<>(wordDict);
        for (int i=1; i<=s.length(); i++){
            for(int j=0; j<i; j++){
                if(dp[j]==true && set.contains(s.substring(j, i))){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}
